/*jslint browser: true*/
/*global $, jQuery, alert, window*/
function get_today() {
    "use strict";
    var today = new Date(),
        data_list = [];
    data_list.push(today.getFullYear());
    data_list.push(today.getMonth() + 1);
    data_list.push(today.getDate());
    return data_list.join("/");
}

window.view = {};
window.view.set = {
    "paper_title": function (str_value) {
        "use strict";
        $("#paper_title").html(str_value);
    },
    "full_mark": function (str_value) {
        "use strict";
        $("#full_mark").html(str_value);
    },
    "pass_score": function (str_value) {
        "use strict";
        $("#pass_score").html(str_value);
    },
    "date": function (str_value) {
        "use strict";
        $("#date").html(str_value);
    },
    "time_limit": function (str_value) {
        "use strict";
        $("#time_limit").html(str_value);
    },
    "time_rest": function (str_value) {
        "use strict";
        $("#time_rest").html(str_value);
    }
};
window.sync = {
    "paper_data_html": function () {
        "use strict";
        var paper_data = window.format.s2c(window.storage(window.paper_key),
                                           window.format.paper)[0];
        window.view.set.paper_title(paper_data.name);
        window.view.set.full_mark(paper_data.score);
        window.view.set.pass_score(paper_data.pass_score);
        window.view.set.date(get_today());
        window.view.set.time_limit(paper_data.time_limit);
        window.view.set.time_rest(paper_data.time_limit);
    },
    "question_data_html": function () {
        "use strict";
        var detail = window.format.s2c(window.storage(window.detail_key),
                                            window.format.detail)[0],
            desp_dict = window.desp_dict,
            hat_dict = detail.hat,
            question_list = detail.question,
            answer_dict = detail.answer || {},
            index = 0,
            length = question_list.length,
            item,
            saved_type,
            table_id,
            format_str = '<h5 class="teste_title" id="{type}_title">{detail}</h5>' +
                '<table class="test_detail" id="{type}_detail">',
            image_str = '<p><a target="_blank" href="/link/{id}/image/">图片链接</a></p>',
            hat_data;
        while (index < length) {
            item = question_list[index];
            /*{id:1, hat:0, type:"", body:, img:, opt:, len:, score:,},
              {id:2, hat:2, type:"", body:, img:, opt:, len:, score:,},*/
            saved_type = item.type;
            table_id = "#{type}_detail".format({type: item.type});
            $(format_str.format({
                type: item.type,
                detail: desp_dict[item.type]
            })).appendTo("#detail_container");
            while (index < length) {
                item = question_list[index];
                if (item.type !== saved_type) {
                    break;
                }
                if (item.hat !== 0) {
                    hat_data = hat_dict[item.hat];
                    /*hat: {"1": {body:, img:,},
                            "2": {body:, img:,}, }*/
                    $('<tr><td colspan="2">{body}{image}</td></tr>'.format({
                        body: hat_data.body,
                        image: hat_data.img ? image_str.format({id: hat_data.img}) : ""
                    })).appendTo(table_id);
                }
                $('<tr><td class="body">' +
                    '{body}{image}<p>{options}</p><p>（{score}分）</p>'.format({
                        body: item.body,
                        image: item.img ? image_str.format({id: item.img}) : "",
                        options: item.opt,
                        score: item.score
                    }) +
                    '</td><td>{count}</td></tr>'.format({
                        count: window.get_answer_radio(item.id, item.len)
                    })).appendTo(table_id);
                //loop
                index += 1;
            }
        }
        $('<div class="space"></div>').appendTo("#detail_container");
    }
};
window.init = {
    "global_var": function () {
        "use strict";
        window.major_all_key = "/major/";
        var category_list = window.category_list,
            level_list = window.level_list,
            pathname = location.pathname,
            paper_reg = new RegExp('^/manager/paper/([0-9]+)/detail/$'),
            reg_result_list = paper_reg.exec(pathname),
            paper_data;
        if (reg_result_list) {
            //update question
            window.is_valid = true;
            window.paper_id = parseInt(reg_result_list[1], 10);
            window.paper_key = "/paper/{id}/".format({id: window.paper_id});
            paper_data = window.format.s2c(window.storage(window.paper_key),
                                           window.format.paper)[0];
            window.detail_key = "/paper/detail/{id}/".format({id: paper_data.detail_id});
        } else {
            window.is_valid = false;
        }
    },
    "paper_data": function () {
        "use strict";
        window.sync.paper_data_html();
    },
    "question_data": function () {
        "use strict";
        window.sync.question_data_html();
    },
    "end_init": function () {
        "use strict";
        window.hideLoading();
    },
    "addEventListener": function () {
        "use strict";
        $("#paper_close").click(window.listener.close_page);
    }
};
window.init_func_list = ["global_var",
                         "paper_data",
                         "question_data",
                         "end_init",
                         "addEventListener"];
window.listener = {
    "close_page": function () {
        "use strict";
        window.close();
    }
};